package dome1;


import java.util.*;

public class BinaryTree {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public int preIndex = 0;

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder, inorder, 0, inorder.length - 1);
    }

    private TreeNode buildTreeChild(int[] preorder, int[] inorder, int inbegin, int inend) {
        if (inbegin > inend) return null;
        TreeNode root = new TreeNode(preorder[preIndex]);
        int rootIndex = find(inorder, inbegin, inend, preorder[preIndex]);
        preIndex++;
        root.left = buildTreeChild(preorder, inorder, inbegin, rootIndex - 1);
        root.right = buildTreeChild(preorder, inorder, rootIndex + 1, inend);
        return root;
    }

    private int find(int[] inorder, int inbegin, int inend, int key) {
        for (int i = inbegin; i <= inend; i++) {
            if (inorder[i] == key) return i;
        }
        return -1;
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || p == root || q == root) return root;
        TreeNode leftTree = lowestCommonAncestor(root.left, p, q);
        TreeNode rightTree = lowestCommonAncestor(root.right, p, q);
        if (leftTree != null && rightTree != null) {
            return root;
        } else if (leftTree != null) {
            return leftTree;
        } else {
            return rightTree;
        }
    }

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        if (root == null) return lists;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
                size--;
            }
            lists.add(list);
        }
        Collections.reverse(lists);
        return lists;
    }

    public String tree2str(TreeNode root) {
        StringBuffer stringBuffer = new StringBuffer();
        tree2strChild(root,stringBuffer);
        return stringBuffer.toString();
    }

    private void tree2strChild(TreeNode root, StringBuffer stringBuffer) {
        if(root == null) return;
        stringBuffer.append(root.val);
        if(root.left!=null) {
            stringBuffer.append(")");
            tree2strChild(root.left,stringBuffer);
            stringBuffer.append(")");
        }else {
            if(root.right!=null) {
                stringBuffer.append("()");
            }else {
                return;
            }
        }
        if(root.right!=null) {
            stringBuffer.append("(");
            tree2strChild(root.right,stringBuffer);
            stringBuffer.append(")");
        }else {
            return;
        }
    }
}








